package org.example.schoolbus.controller;
import org.example.schoolbus.entity.ResultBody;
import org.example.schoolbus.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("/Login")
@CrossOrigin(origins = "*")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @RequestMapping("/LoginUser")
    public ResultBody LoginUser(@RequestBody Map<String, Object> map) {
        if (map.get("userstyle") == null || map.get("userphone") == null || map.get("userpassword") == null) {
            return new ResultBody(404,"填写信息不完整",false);
        }
        String userstyle = map.get("userstyle").toString();
        String userphone = map.get("userphone").toString();
        String userpassword = map.get("userpassword").toString();
        switch(userstyle)
        {
            case "0"://"0"是学生
                if(loginService.LoginUser(userphone,userpassword)==1)
                {//核实用户电话号码不存在
                    return new ResultBody(400,"用户手机号错误",false);
                }
                else if(loginService.LoginUser(userphone,userpassword)==2)
                {//电话号码存在时，电话号码和密码匹配
                    return new ResultBody(200,"登录成功",map);
                }
                else if(loginService.LoginUser(userphone,userpassword)==3)
                {
                    return new ResultBody(400,"登录状态更新失败",false);
                }
                else//电话号码存在，电话号码与密码不匹配
                    return new ResultBody(400,"用户密码错误",false);
            case "1"://"1"是管理员
                if(loginService.LoginUser(userphone,userpassword)==1)
                {//核实用户电话号码不存在
                    return new ResultBody(400,"用户手机号错误",false);
                }
                else if(loginService.LoginUser(userphone,userpassword)==2)
                {//电话号码存在时，电话号码和密码匹配
                    return new ResultBody(200,"登录成功",map);
                }
                else if(loginService.LoginUser(userphone,userpassword)==3)
                {
                    return new ResultBody(400,"登录状态更新失败",false);
                }
                else//电话号码存在，电话号码与密码不匹配
                    return new ResultBody(400,"用户密码错误",false);
            default:
                return new ResultBody(400,"未知用户类型",false);
        }
    }
}
